<?php


class SingleSignModel
{
    const Redis_Key = "usertoken-";

    private $drive;

    private $userModel;

    public function __construct(UserModel $userModel)
    {
        $this->drive = new \Redis();
        $this->drive->connect('127.0.0.1', 6379);
        $this->userModel = $userModel;
    }


    /**
     * token and 过期时间
     * @param $token
     * @param $expTime
     */
    public function sava($token, $expTime)
    {
        $this->drive->setex(self::Redis_Key . $this->userModel->getId(), $expTime - time(), $token);
    }


    /**
     * 验证token
     * @param $token
     * @return bool
     */
    public function check($token)
    {
        $userToken = $this->drive->get(self::Redis_Key . $this->userModel->getId());
        if ($userToken) {
            return $userToken == $token;
        }
        return false;
    }


}